home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / xwinc100.zip / CONTRIB-.00 / CONTRIB- / contrib / examples / Xaw / xwidgets.c < prev   
C/C++ Source or Header  |  1991-01-22  |  13KB  |  445 lines

  1. /*
  2.  * This example uses all the Athena widgets, and demonstrates timeouts.
  3.  *
  4.  * November 30, 1989 - Chris D. Peterson 
  5.  */
  6.  
  7. /*
  8.  * $XConsortium: xwidgets.c,v 1.29 91/01/22 20:02:39 gildea Exp $
  9.  *
  10.  * Copyright 1989 Massachusetts Institute of Technology
  11.  *
  12.  * Permission to use, copy, modify, distribute, and sell this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided that
  14.  * the above copyright notice appear in all copies and that both that
  15.  * copyright notice and this permission notice appear in supporting
  16.  * documentation, and that the name of M.I.T. not be used in advertising or
  17.  * publicity pertaining to distribution of the software without specific,
  18.  * written prior permission.  M.I.T. makes no representations about the
  19.  * suitability of this software for any purpose.  It is provided "as is"
  20.  * without express or implied warranty.
  21.  *
  22.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  24.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  25.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  26.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  27.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include <X11/Xatom.h>
  33. #include <X11/Intrinsic.h>
  34. #include <X11/StringDefs.h>
  35.  
  36. #include <X11/Xaw/AsciiText.h>
  37. #include <X11/Xaw/Box.h>
  38. #include <X11/Xaw/Clock.h>
  39. #include <X11/Xaw/Command.h>
  40. #include <X11/Xaw/Dialog.h>
  41. #include <X11/Xaw/Form.h>
  42. #include <X11/Xaw/Label.h>
  43. #include <X11/Xaw/List.h>
  44. #include <X11/Xaw/Logo.h>
  45. #include <X11/Xaw/MenuButton.h>
  46. #include <X11/Xaw/Scrollbar.h>
  47. #include <X11/Xaw/SimpleMenu.h>
  48. #include <X11/Xaw/SmeBSB.h>
  49. #include <X11/Xaw/SmeLine.h>
  50. #include <X11/Xaw/StripChart.h>
  51. #include <X11/Xaw/Paned.h>
  52. #include <X11/Xaw/Toggle.h>
  53. #include <X11/Xaw/Viewport.h>
  54.  
  55. #include <X11/Xaw/Cardinals.h>
  56.  
  57. String fallback_resources[] = { 
  58.     "*input:                  True",
  59.     "*Paned.width:            350",
  60.     "*label.label:            At least one of each Athena Widget.",
  61.     "*Dialog.label:           I am a Dialog widget.",
  62.     "*Dialog.value:           Enter new value here.",
  63.     "*Dialog*command*label:   ok",
  64.     "*Dialog*resizable:       True",
  65.     "*Viewport*allowVert:     True",
  66.     "*Form*formLabel.label:   0",
  67.     "*Form*resizable:       True",
  68.     "*StripChart*update:      1",
  69.     "*StripChart*jumpScroll:  1",
  70.     "*Box*allowResize:        True",
  71.     "*scrollbar*orientation:  horizontal",
  72.     "*scrollbar*length:       100",
  73.     "*text*height:            75",
  74.     "*text*string:            Look ma,\\na text widget!",
  75.     "*text*editType:          edit",
  76.     "*text*scrollVertical:    whenNeeded",
  77.     "*text*scrollHorizonal:   whenNeeded",
  78.     "*textFile*type:          file",
  79.     "*textFile*string:        /etc/motd",
  80.     "*textFile*scrollVertical:    whenNeeded",
  81.     "*textFile*scrollHorizonal:   whenNeeded",
  82.     "*textFile*height:        75",
  83.     NULL,
  84. };
  85.  
  86. static void Destroyed(), Quit(), Count(), Syntax(), CreateFormWithButtons();
  87. static void CreateBox(), Okay(), Scrolled(), Thumbed(), Finish();
  88. static void TimeSinceTouched();
  89.  
  90. void 
  91. main(argc, argv)
  92. int argc;
  93. char **argv;
  94. {
  95.     Widget toplevel, outer, dialog, box, quit, chart, list, viewport;
  96.     XtAppContext app_con;
  97.  
  98.     toplevel = XtAppInitialize(&app_con, "Xwidgets", NULL, ZERO,
  99.                    &argc, argv, fallback_resources,
  100.                    NULL, ZERO);
  101.  
  102.     if (argc != 1) 
  103.     Syntax(app_con, argv[0]);
  104.  
  105.     XtAddCallback( toplevel, XtNdestroyCallback, Destroyed, NULL );
  106.  
  107.     outer = XtCreateManagedWidget( "paned", panedWidgetClass, toplevel,
  108.                   NULL, ZERO);
  109.  
  110.     quit = XtCreateManagedWidget( "quit", commandWidgetClass, outer, 
  111.                  NULL, ZERO);
  112.     XtAddCallback( quit, XtNcallback, Quit, (XtPointer) toplevel);
  113.  
  114.     (void)XtCreateManagedWidget( "label", labelWidgetClass, outer, NULL, ZERO);
  115.     
  116.     dialog = XtCreateManagedWidget("dialog", dialogWidgetClass, outer, 
  117.                    NULL, ZERO);
  118.  
  119.     viewport = XtCreateManagedWidget( "viewport", viewportWidgetClass, outer,
  120.                      NULL, ZERO);
  121.  
  122.     list = XtCreateManagedWidget("list", listWidgetClass, viewport, 
  123.                  NULL, ZERO);
  124.  
  125.     XawDialogAddButton(dialog, "command", Okay, (XtPointer) list);
  126.  
  127.  
  128.     CreateFormWithButtons(outer);
  129.  
  130.     box = XtCreateManagedWidget( "box", boxWidgetClass, outer, NULL, ZERO);
  131.     (void) XtCreateManagedWidget(NULL, clockWidgetClass, box, NULL, ZERO);
  132.     (void) XtCreateManagedWidget(NULL, logoWidgetClass, box, NULL, ZERO);
  133.  
  134.     chart = XtCreateManagedWidget("stripChart", stripChartWidgetClass, outer,
  135.                   NULL, ZERO);
  136.     XtAddCallback(chart, XtNgetValue, TimeSinceTouched, NULL);
  137.  
  138.     (void) XtCreateManagedWidget("text", asciiTextWidgetClass,outer,NULL,ZERO);
  139.  
  140.     CreateBox(outer);
  141.  
  142.     (void) XtCreateManagedWidget("textFile", asciiTextWidgetClass, outer, 
  143.                  NULL, ZERO);
  144.  
  145.     XtRealizeWidget(toplevel);
  146.     XtAppMainLoop(app_con);
  147. }
  148.  
  149. /*    Function Name: CreateFormWithButtons
  150.  *    Description: Creates a form widget that contains:
  151.  *                   a label, command, toggle, and menu button.
  152.  *    Arguments: parent - the parent of the form widget.
  153.  *    Returns: none.
  154.  */
  155.  
  156. static void
  157. CreateFormWithButtons(parent)
  158. Widget parent;
  159. {
  160.     Widget form, label, button, menu;
  161.     int i;
  162.     Arg args[1];
  163.  
  164.     form = XtCreateManagedWidget( "form", formWidgetClass, parent,
  165.                  NULL, ZERO );
  166.  
  167.     label = XtCreateManagedWidget("formLabel",labelWidgetClass,form,NULL,ZERO);
  168.  
  169.     XtSetArg(args[0], XtNfromHoriz, label);
  170.     button = XtCreateManagedWidget("command",commandWidgetClass,form,args,ONE);
  171.     XtAddCallback( button, XtNcallback, Count, (XtPointer) label );
  172.  
  173.     XtSetArg(args[0], XtNfromHoriz, button);
  174.     button = XtCreateManagedWidget("toggle", toggleWidgetClass, form,args,ONE);
  175.     XtAddCallback( button, XtNcallback, Count, (XtPointer) label );
  176.  
  177.     XtSetArg(args[0], XtNfromHoriz, button);
  178.     button = XtCreateManagedWidget("menuButton", menuButtonWidgetClass, form,
  179.                    args, ONE);
  180.  
  181.     menu = XtCreatePopupShell("menu", simpleMenuWidgetClass, button,NULL,ZERO);
  182.  
  183.     for (i = 0; i < 5; i++) {
  184.     char buf[BUFSIZ];
  185.     sprintf(buf, "menuEntry%d", i + 1);
  186.     (void) XtCreateManagedWidget(buf, smeBSBObjectClass, menu,
  187.                      NULL, ZERO);
  188.     if (i == 2)
  189.         (void) XtCreateManagedWidget("menuButton", smeLineObjectClass, 
  190.                      menu, NULL, ZERO);
  191.     }
  192. }
  193.  
  194. /*    Function Name: CreateBox
  195.  *    Description: Creates a box with a label and a scrollbar in it.
  196.  *    Arguments: parent - the parent of the box.
  197.  *    Returns: none.
  198.  */
  199.  
  200. static void
  201. CreateBox(parent)
  202. Widget parent;
  203. {
  204.     Widget box, scrollbar, label;
  205.     
  206.     box = XtCreateManagedWidget(NULL, boxWidgetClass, parent, NULL, ZERO);
  207.  
  208.     scrollbar = XtCreateManagedWidget("scrollbar", scrollbarWidgetClass, box,
  209.                       NULL, ZERO);
  210.  
  211.     label = XtCreateManagedWidget("scroll_label", labelWidgetClass, box, 
  212.                   NULL, ZERO);
  213.  
  214.     XtAddCallback(scrollbar, XtNjumpProc, Thumbed, (XtPointer) label);
  215.     XtAddCallback(scrollbar, XtNscrollProc, Scrolled, (XtPointer) label);
  216. }
  217.  
  218. /*    Function Name: Syntax
  219.  *    Description: Prints a the calling syntax for this function to stdout.
  220.  *    Arguments: app_con - the application context.
  221.  *                 call - the name of the application.
  222.  *    Returns: none - exits tho.
  223.  */
  224.  
  225. static void 
  226. Syntax(app_con, call)
  227. XtAppContext app_con;
  228. char *call;
  229. {
  230.     XtDestroyApplicationContext(app_con);
  231.     fprintf(stderr, "Usage: %s\n", call);
  232.     exit(1);
  233. }
  234.  
  235. /*    Function Name: TimeSinceTouched
  236.  *    Description: This function returns the number of 10s of seconds since
  237.  *                   the user caused an event in this application.
  238.  *    Arguments: widget - the strip chart widget.
  239.  *                 closure - unused.
  240.  *                 value_ptr - a pointer to the value to return.
  241.  *    Returns: none
  242.  */
  243.  
  244. /* ARGSUSED */
  245. static void 
  246. TimeSinceTouched(widget,closure,value_ptr)
  247. Widget widget;
  248. XtPointer closure, value_ptr;
  249. {
  250.     double *value = (double *) value_ptr;
  251.     static double old_value = 0.0;
  252.     static Time old_stamp = 1;
  253.     Time new_stamp;
  254.  
  255.     new_stamp = XtLastTimestampProcessed(XtDisplay(widget));
  256.  
  257.     if (old_stamp != new_stamp) {
  258.     old_stamp = new_stamp;
  259.     old_value = 0.0;
  260.     }
  261.     else {
  262.     Arg args[1];
  263.     int update;
  264.  
  265.     XtSetArg(args[0], XtNupdate, &update);
  266.     XtGetValues(widget, args, ONE);
  267.     old_value += (double) update / 10.0;
  268.     }
  269.  
  270.     *value = old_value;
  271.  
  272. }
  273.  
  274. /*    Function Name: Quit
  275.  *    Description: Destroys all widgets, and returns.
  276.  *    Arguments: widget - the widget that called this callback.
  277.  *                 closure, callData - *** UNUSED ***.
  278.  *    Returns: none
  279.  * 
  280.  * NOTE:  The shell widget has a destroy callback that sets a five second
  281.  *        timer, and at the end of that time the program exits.
  282.  */
  283.  
  284. /* ARGSUSED */
  285. static void 
  286. Quit(widget,closure,callData)
  287. Widget widget;
  288. XtPointer closure, callData;
  289. {
  290.     fprintf(stderr, "command callback.\n");
  291.     XtDestroyWidget((Widget)closure);
  292. }
  293.  
  294. /*    Function Name: Count
  295.  *    Description: This callback routin will increment that counter
  296.  *                   and display the number as the label of the widget passed 
  297.  *                   in the closure.
  298.  *    Arguments: widget - *** UNUSED ***
  299.  *                 closure - a pointer to the label widge to display the 
  300.  *                           string in.
  301.  *                 callData - *** UNUSED ***
  302.  *    Returns: none
  303.  */
  304.  
  305. /* ARGSUSED */
  306. static void 
  307. Count(widget, closure, callData)
  308. Widget widget;
  309. XtPointer closure, callData;
  310. {
  311.    Arg arg[1];
  312.    char text[10];
  313.    static int count = 0;
  314.  
  315.    sprintf( text, " %d ", ++count );
  316.    XtSetArg( arg[0], XtNlabel, text );
  317.    XtSetValues( (Widget)closure, arg, ONE );
  318. }
  319.  
  320. /*    Function Name: Okay
  321.  *    Description: Adds an entry to the List widget passed as clientData.
  322.  *                   The name of this entry is the value of the dialog widget.
  323.  *    Arguments: widget - the button that called this callback.
  324.  *                 clientData - the list widget to add the entry to.
  325.  *                 callData - *** UNUSED ***.
  326.  *    Returns: none.
  327.  *
  328.  * NOTE: It is assumed that the button is a child of the dialog.
  329.  */
  330.  
  331. /* ARGSUSED */
  332. static void 
  333. Okay(widget, clientData, callData)
  334. Widget widget;
  335. XtPointer clientData, callData;
  336. {
  337.     Widget list_widget = (Widget) clientData;
  338.     static String * list = NULL;
  339.     static int num_items = 0, allocated_items = 0;
  340.  
  341.     if (num_items == allocated_items) {
  342.     allocated_items += 10;
  343.     list = (String *) XtRealloc((char *)list, sizeof(String) * allocated_items);
  344.     }
  345.  
  346.     /*
  347.      * The value returned by XawDialogGetValueString() does not remain good
  348.      * forever so we must store is somewhere.  This creates a memory leak 
  349.      * since I don't remember these values, and free them later.  I know about
  350.      * it, but it doesn't seem worth fixing in this example.
  351.      */
  352.  
  353.     list[num_items++] = XtNewString(XawDialogGetValueString(XtParent(widget)));
  354.     XawListChange(list_widget, list, num_items, 0, True);
  355. }
  356.  
  357. /*    Function Name: Scrolled.
  358.  *    Description: Prints the location of the pointer into the label
  359.  *                   widget provided.
  360.  *    Arguments: widget - *** UNUSED ***.
  361.  *                 label_ptr - a pointer to the label widget.
  362.  *                 value_ptr - the amount the bar has been scrolled.
  363.  *    Returns: none
  364.  */
  365.  
  366. /* ARGSUSED */
  367. static void 
  368. Scrolled(widget, label_ptr, value_ptr)
  369. Widget widget;
  370. XtPointer label_ptr, value_ptr;    
  371. {
  372.     int value = (int) value_ptr;
  373.     Widget label = (Widget) label_ptr;
  374.     Arg args[1];
  375.     char message[BUFSIZ];
  376.  
  377.     sprintf( message, " Scrolled by %d pixels", value);
  378.     XtSetArg( args[0], XtNlabel, message );
  379.     XtSetValues( label, args, ONE );
  380. }
  381.  
  382. /*    Function Name: Thumbed.
  383.  *    Description: Prints the location of the thumb as a percentage of the 
  384.  *                   height of the scrollbar into the label widget provided.
  385.  *    Arguments: widget - *** UNUSED ***.
  386.  *                 label_ptr - a pointer to the label widget.
  387.  *                 top_ptr - a pointer to a float containing the location of 
  388.  *                           of the scrollbar's thumb.
  389.  *    Returns: 
  390.  */
  391.  
  392. /* ARGSUSED */
  393. static void 
  394. Thumbed(widget, label_ptr, top_ptr)
  395. Widget widget;
  396. XtPointer label_ptr, top_ptr;    
  397. {
  398.     float top = *((float *) top_ptr);
  399.     Widget label = (Widget) label_ptr;
  400.     Arg args[1];
  401.     char message[BUFSIZ];
  402.  
  403.     sprintf( message, " Thumbed to %d%% ", (int)(top*100) );
  404.     XtSetArg( args[0], XtNlabel, message );
  405.     XtSetValues( label, args, ONE );
  406. }
  407.  
  408. /*    Function Name: Finish
  409.  *    Description: This is a timeout function that will exit the program.
  410.  *    Arguments: client_data, id - *** UNUSED ***.
  411.  *    Returns: exits.
  412.  */
  413.  
  414. /* ARGSUSED */
  415. static void
  416. Finish(client_data, id)
  417. XtPointer client_data;
  418. XtIntervalId *id;
  419. {
  420.     /*
  421.      * I should really destroy the app_context here, but I am lazy, and
  422.      * getting the app_context is a pain.
  423.      */
  424.     fprintf(stderr, "Bye!\n");
  425.     exit(0);
  426. }
  427.  
  428. /*    Function Name: Destroyed
  429.  *    Description: This is a Destroy callback that when called sets a 
  430.  *                   timeout function that will exit the program in 5 seconds.
  431.  *    Arguments: widget - the widget that was just destroyed.
  432.  *                 closure, callData - UNUSED.
  433.  *    Returns: none.
  434.  */
  435.  
  436. /* ARGSUSED */
  437. static void 
  438. Destroyed(widget, closure, callData)
  439. Widget widget;
  440. XtPointer closure, callData;        
  441. {
  442.     fprintf( stderr, "Everything now destroyed; setting 5 second timer.\n" );
  443.     XtAppAddTimeOut(XtWidgetToApplicationContext(widget), 5000, Finish, NULL);
  444. }
  445.